package cn.gtlc.util;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author zhouwx
 * @Date 2022/6/8
 * @Version -
 */
public class HttpClientUtils{

    // 通用HTTP请求方法
    public static String clientUI(HttpRequestBase httpRequestBase){
        String str = null;
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpRequestBase);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                str = EntityUtils.toString(responseEntity,"UTF-8");
                // System.out.println("响应内容为:" + str);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return str;
    }

    public static void main(String[] args) {

        CloseableHttpClient aDefault = HttpClients.createDefault(); // 方式一
        CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 方式二

        // POST请求
        URI uri = null;
        try {
            uri = new URIBuilder().setScheme("http").setHost("localhost").setPath("/请求路径").build();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        HttpPost httpPost = new HttpPost(uri);
        httpPost.setHeader("Content-Type","application/x-www-form-urlencoded");
        httpPost.setHeader("charset", StandardCharsets.UTF_8.name());
        List<NameValuePair> params2 = new ArrayList<>();
        params2.add(new BasicNameValuePair("edit","fanyuan_down"));
/*        params2.add(new BasicNameValuePair("name",ajkHouse.getName()));
        params2.add(new BasicNameValuePair("types",type2));
        params2.add(new BasicNameValuePair("houseId",ajkHouse.getUnityInfoId()));*/
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(params2,"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String s = HttpClientUtils.clientUI(httpPost);
    }


}



